package com.duolebo.uteped_sdk.msg;

import androidx.annotation.NonNull;

import com.duolebo.uteped_sdk.UTEPedEventHandler;
import com.duolebo.uteped_sdk.utils.Const;
import com.duolebo.uteped_sdk.utils.DeviceHelper;
import com.duolebo.uteped_sdk.utils.PermissionHelper;
import com.yc.nadalsdk.bean.ContactsInfo;
import com.yc.nadalsdk.bean.Response;
import com.yc.nadalsdk.bean.SmsReplyInfo;
import com.yc.nadalsdk.bean.SosCallInfo;
import com.yc.nadalsdk.ble.open.DeviceModeJX;
import com.yc.nadalsdk.ble.open.UteBleConnection;
import com.yc.nadalsdk.listener.ContactsSyncListener;
import com.yc.nadalsdk.listener.SmsReplySyncListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class ContactMessage extends BaseMessage implements ContactsSyncListener, SmsReplySyncListener {

    private static final String PATH = "contact";
    private static final String M_syncContactsInfoToDevice = "syncContactsInfoToDevice";
    private static final String M_syncSosCallInfo = "syncSosCallInfo";
    private static final String M_syncSmsReplyContent = "syncSmsReplyContent";
    private static final String M_smsReplyContentCount = "smsReplyContentCount";

    /**
     * BaseCallback
     *
     * @param methodChannel methodChannel
     * @param eventHandler eventHandler
     */
    public ContactMessage(MethodChannel methodChannel, UTEPedEventHandler eventHandler) {
        super(methodChannel, eventHandler);
    }

    @Override
    public String getPath() {
        return PATH;
    }

    @Override
    public boolean methodCallConsumed(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        if (!call.hasArgument(Const.KEY_METHOD))
            return false;

        if (super.methodCallConsumed(call, result))
            return true;

        UteBleConnection uteBleConnection = uteBleClient.getUteBleConnection();
        boolean consumed = true;
        String method = call.argument(Const.KEY_METHOD);
        assert method != null;

        boolean enable;
        boolean success = false;
        Response<?> rsp;
        Map<String, Object> map = new HashMap<>();

        switch (method) {
            case M_syncContactsInfoToDevice:
                if (uteBleClient.isConnected()) {
                    rsp = uteBleConnection.syncContactsInfoToDevice(getContactList(call), this);
                    success = rsp.getErrorCode() == Const.CODE_SUCCESS;
                }
                result.success(success);
                break;

            case M_syncSosCallInfo:
                if (uteBleClient.isConnected()) {
                    rsp = uteBleConnection.syncSosCallInfo(getSosCallInfo(call));
                    success = rsp.getErrorCode() == Const.CODE_SUCCESS;
                }
                result.success(success);
                break;

            case Const.M_setSmsReplyEnable:
                if (uteBleClient.isConnected()) {
                    enable = Boolean.TRUE.equals(call.argument(Const.KEY_ENABLE));
                    rsp = uteBleConnection.setSmsReplyEnable(enable);
                    success = rsp.getErrorCode() == Const.CODE_SUCCESS;
                }
                result.success(success);
                break;

            case Const.M_setSmsRemindEnable:
                enable = Boolean.TRUE.equals(call.argument(Const.KEY_ENABLE));
                if (enable && !PermissionHelper.hasSmsPermissions()) {
                    onCheckPermission(Const.P_sms);
                    result.success(false);
                }
                else {
                    DeviceHelper.getInstance().save(Const.M_setSmsRemindEnable, enable);
                    result.success(true);
                }
                break;

            case Const.M_setCallRemindEnable:
                enable = Boolean.TRUE.equals(call.argument(Const.KEY_ENABLE));
                if (uteBleClient.isConnected()) {
                    rsp = uteBleConnection.setCallRemindEnable(enable);
                    success = (rsp.getErrorCode() == Const.CODE_SUCCESS);
                    if (success) {
                        DeviceHelper.getInstance().save(Const.M_setCallRemindEnable, enable);
                    }
                }
                result.success(success);
                break;

            case Const.M_setCallRemindHangup:
                enable = Boolean.TRUE.equals(call.argument(Const.KEY_ENABLE));
                DeviceHelper.getInstance().save(Const.M_setCallRemindEnable, enable);
                result.success(true);
                break;

            case M_syncSmsReplyContent:
                if (uteBleClient.isConnected()) {
                    rsp = uteBleConnection.syncSmsReplyContent(getSmsReplyList(call), this);
                    map.put(Const.KEY_SUCCESS, rsp.getErrorCode() == Const.CODE_SUCCESS);
                    map.put(Const.KEY_ERROR_CODE, rsp.getErrorCode());
                }
                result.success(map);
                break;

            case M_smsReplyContentCount:
                result.success(getSmsReplyMaxCount());
                break;

            default:
                consumed = false;
        }

        return consumed;
    }

    private List<ContactsInfo> getContactList(MethodCall call) {
        List<ContactsInfo> contactsList = new ArrayList<>();
        if (call.hasArgument(Const.TYPE_LIST)) {
            List<Map<String, Object>> mapList = call.argument(Const.TYPE_LIST);
            assert mapList != null;
            for (Map<String, Object> mapItem:mapList) {
                ContactsInfo contactsInfo = new ContactsInfo();
                if (mapItem.containsKey(Const.KEY_NAME)) {
                    contactsInfo.setName((String) mapItem.get(Const.KEY_NAME));
                }
                if (mapItem.containsKey(Const.KEY_PHONE)) {
                    contactsInfo.setPhone((String) mapItem.get(Const.KEY_PHONE));
                }
                if (mapItem.containsKey(Const.KEY_NOTE)) {
                    contactsInfo.setNote((String) mapItem.get(Const.KEY_NOTE));
                }

                contactsList.add(contactsInfo);
            }
        }

        return contactsList;
    }

    @Override
    public void onContactsSyncState(int state) {
        Map<String, Object> map = new HashMap<>();
        map.put(Const.KEY_PATH, getPath());
        map.put(Const.KEY_METHOD, M_syncContactsInfoToDevice);
        map.put(Const.KEY_ACTION, Const.ACTION_SYNC_CONTACT_STATE);
        map.put(Const.KEY_VALUE, state);
        eventHandler.publishEvent(map);
    }

    @Override
    public void onContactsSyncProgress(int progress) {
        Map<String, Object> map = new HashMap<>();
        map.put(Const.KEY_PATH, getPath());
        map.put(Const.KEY_METHOD, M_syncContactsInfoToDevice);
        map.put(Const.KEY_ACTION, Const.ACTION_SYNC_CONTACT_PROGRESS);
        map.put(Const.KEY_VALUE, progress);
        eventHandler.publishEvent(map);
    }

    private List<SosCallInfo> getSosCallInfo(MethodCall call) {
        List<SosCallInfo> sosCallInfoList = new ArrayList<>();
        if (call.hasArgument(Const.TYPE_LIST)) {
            List<Map<String, Object>> mapList = call.argument(Const.TYPE_LIST);
            for (Map<String, Object> mapItem : mapList) {
                SosCallInfo sosCallInfo = new SosCallInfo();
                if (mapItem.containsKey(Const.KEY_NAME)) {
                    sosCallInfo.setName((String) mapItem.get(Const.KEY_NAME));
                }
                if (mapItem.containsKey(Const.KEY_PHONE)) {
                    sosCallInfo.setPhone((String) mapItem.get(Const.KEY_PHONE));
                }

                sosCallInfoList.add(sosCallInfo);
            }
        }

        return sosCallInfoList;
    }

    private List<SmsReplyInfo> getSmsReplyList(MethodCall call) {
        List<SmsReplyInfo> smsReplyList = new ArrayList<>();
        if (call.hasArgument(Const.TYPE_LIST)) {
            List<String> smsList = call.argument(Const.TYPE_LIST);
            int index = 0;
//            if (DeviceHelper.getInstance().isJuXin()) {
//                index = 5;
//            }
            assert smsList != null;
            if (index < smsList.size()) {
                for (int i = index; i < smsList.size();i++) {
                    SmsReplyInfo smsReplyInfo = new SmsReplyInfo(smsList.get(i));
                    smsReplyList.add(smsReplyInfo);
                }
            }
        }

        return smsReplyList;
    }

    private int getSmsReplyMaxCount() {
//        return DeviceModeJX.isHasFunction_7(DeviceModeJX.IS_PLATFORM_JLAC701) ? 3 : 3;
        return 3;
    }

    @Override
    public void onSmsReplySyncState(int state) {
        Map<String, Object> map = new HashMap<>();
        map.put(Const.KEY_PATH, getPath());
        map.put(Const.KEY_METHOD, M_syncSmsReplyContent);
        map.put(Const.KEY_ACTION, Const.ACTION_SYNC_SMS_REPLY_STATE);
        map.put(Const.KEY_VALUE, state);
        eventHandler.publishEvent(map);
    }

    @Override
    public void onSmsReplySyncProgress(int progress) {
        Map<String, Object> map = new HashMap<>();
        map.put(Const.KEY_PATH, getPath());
        map.put(Const.KEY_METHOD, M_syncSmsReplyContent);
        map.put(Const.KEY_ACTION, Const.ACTION_SYNC_SMS_REPLY_PROGRESS);
        map.put(Const.KEY_VALUE, progress);
        eventHandler.publishEvent(map);
    }
}
